package com.xuexiang.xuidemo.slice.picker;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

import com.xuexiang.xui_lib.component.picker.XRangeSlider;
import com.xuexiang.xui_lib.component.picker.XSeekBar;
import com.xuexiang.xuidemo.ResourceTable;

/**
 * SeekBar
 * 滑块选择器，支持双向范围选择
 */
public class SeekBarAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Text text_title,text_title_bottom;
    private Image image_back;
    private XSeekBar xsb;
    private XRangeSlider xrsBubble;
    private XRangeSlider xrsNormal;
    private Text tvXsb,tvBubble,tvNumber;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_seekbar);
        initComponent();
        initTitle();
        initClickedListener();
    }
    public void  initComponent(){
        image_back=(Image) findComponentById(ResourceTable.Id_image_back);
        text_title=(Text)findComponentById(ResourceTable.Id_text_title);
        text_title_bottom=(Text)findComponentById(ResourceTable.Id_text_title_bottom);

        xrsBubble=(XRangeSlider)findComponentById(ResourceTable.Id_xrs_bubble);
        tvBubble=(Text)findComponentById(ResourceTable.Id_tv_bubble);

        xrsBubble.setStartingMinMax(60, 100);
        xrsBubble.setOnRangeSliderListener(new XRangeSlider.OnRangeSliderListener() {
            @Override
            public void onMaxChanged(XRangeSlider slider, int maxValue) {
                tvBubble.setText(String.format("%d     %d", slider.getSelectedMin(), slider.getSelectedMax()));
            }

            @Override
            public void onMinChanged(XRangeSlider slider, int minValue) {
                tvBubble.setText(String.format("%d     %d", slider.getSelectedMin(), slider.getSelectedMax()));
            }
        });

        xrsNormal=(XRangeSlider)findComponentById(ResourceTable.Id_xrs_normal);
        tvNumber=(Text)findComponentById(ResourceTable.Id_tv_number);
        xrsNormal.setOnRangeSliderListener(new XRangeSlider.OnRangeSliderListener() {
            @Override
            public void onMaxChanged(XRangeSlider slider, int maxValue) {
                tvNumber.setText(String.format("%d     %d", slider.getSelectedMin(), slider.getSelectedMax()));
            }

            @Override
            public void onMinChanged(XRangeSlider slider, int minValue) {
                tvNumber.setText(String.format("%d     %d", slider.getSelectedMin(), slider.getSelectedMax()));
            }
        });


        xsb=(XSeekBar)findComponentById(ResourceTable.Id_xsb);
        tvXsb=(Text)findComponentById(ResourceTable.Id_tv_xsb);
        xsb.setDefaultValue(50);
        xsb.setOnSeekBarListener(new XSeekBar.OnSeekBarListener() {
            @Override
            public void onValueChanged(XSeekBar seekBar, int newValue) {
                tvXsb.setText(String.valueOf(newValue));
            }
        });
    }
    private void initTitle() {
        text_title.setText("SeekBar");
        text_title_bottom.setText("滑块选择器，支持双向范围选择");
    }
    private void initClickedListener() {
        image_back.setClickedListener(this);
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
        }
    }
}
